package com.rdrrlabs.timeriffic.ui;

import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.RemoteViews;

import com.rdrrlabs.timeriffic.app.UpdateReceiver;
import com.rdrrlabs.timeriffic.app.UpdateService;
import com.rdrrlabs.timeriffic.base.R;
import com.rdrrlabs.timeriffic.core.app.UpdateServiceImpl;

/** Widget UI */
public class Widget extends AppWidgetProvider
{
	private static final boolean DEBUG = true;
	public static final String TAG = Widget.class.getSimpleName();

	@Override
	public void onUpdate(Context context, AppWidgetManager manager, int[] appWidgetIds)
	{
		super.onUpdate(context, manager, appWidgetIds);

		if (DEBUG) Log.d(TAG, "Widget onUpdate - enter");

    // Setup widget to support click
		RemoteViews remoteView = new RemoteViews(context.getPackageName(), R.layout.widget_screen); 
    Intent intent = new Intent(context, UpdateService.class);
		intent.setAction(UpdateReceiver.ACTION_APPLY_STATE);
		intent.putExtra(UpdateServiceImpl.EXTRA_CHECK_NOW_INTENT, true);
		intent.putExtra(UpdateReceiver.EXTRA_TOAST_NEXT_EVENT, UpdateReceiver.TOAST_ALWAYS);
		PendingIntent pendingIntent = PendingIntent.getService(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
		
		remoteView.setOnClickPendingIntent(R.id.widget_layout, pendingIntent);

		manager.updateAppWidget(appWidgetIds, remoteView);
		
		if (DEBUG) Log.d(TAG, "Widget onUpdate - exit");
	}
}